/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.SdBountyHunter.Handlers;

import com.SySammy.SdBountyHunter.PrisonDataBase;
import com.SySammy.SdBountyHunter.sdLaw;
import org.bukkit.Location;
import org.bukkit.entity.Player;

public class PrisonHandler {

    sdLaw plugin;

    public PrisonHandler(sdLaw plugin) {
        this.plugin = plugin;
    }

    private PrisonDataBase OpenDataBase(String p) {
        PrisonDataBase persistenceClass = plugin.getDatabase().find(PrisonDataBase.class).where().ieq("OwnerName", p).findUnique();
        if (persistenceClass == null) {
            persistenceClass = new PrisonDataBase();
        }
        return persistenceClass;
    }

    public boolean HasPrison(String p) {
        PrisonDataBase persistenceClass = plugin.getDatabase().find(PrisonDataBase.class).where().ieq("OwnerName", p).findUnique();
        if (persistenceClass == null) {
            return false;
        }
        return true;
    }

    public void RegPrisonOwner(Player p, String city, boolean server) {
        if (server == true) {
            PrisonDataBase plyReg = OpenDataBase("SERVER");
            plyReg.setCityName("SERVER");
            plyReg.setOwnerName("SERVER");
            plyReg.setRegCount(1);
            plugin.getDatabase().save(plyReg);

        } else {
            PrisonDataBase plyReg = OpenDataBase(p.getName());
            plyReg.setCityName(city);
            plyReg.setOwnerName(p.getName());
            plyReg.setRegCount(1);
            plugin.getDatabase().save(plyReg);
        }
    }

    public void RegPrisonLoc(String p, String world, double x, double y, double z) {
        PrisonDataBase plyReg = OpenDataBase(p);
        plyReg.setWorldName(world);
        plyReg.setPx((int) x);
        plyReg.setPy((int) y);
        plyReg.setPz((int) z);
        plyReg.setRegCount(2);
        plugin.getDatabase().save(plyReg);
    }

    public void RegPrisonSign(String p, String world, double x, double y, double z) {
        PrisonDataBase plyReg = OpenDataBase(p);
        plyReg.setSx((int) x);
        plyReg.setSy((int) y);
        plyReg.setSz((int) z);
        plyReg.setRegCount(3);
        plugin.getDatabase().save(plyReg);
    }

    public void RegPrisonLC(String p, String world, double x, double y, double z) {
        PrisonDataBase plyReg = OpenDataBase(p);
        plyReg.setLCx((int) x);
        plyReg.setLCy((int) y);
        plyReg.setLCz((int) z);
        plyReg.setRegCount(4);
        plugin.getDatabase().save(plyReg);
    }

    public boolean RegPrisonRC(String p, String world, double x, double y, double z) {
        PrisonDataBase plyReg = OpenDataBase(p);
        if (x == plyReg.getLCx() && y == plyReg.getLCy() && z == plyReg.getLCz()) {
            return false;
        }
        plyReg.setRCx((int) x);
        plyReg.setRCy((int) y);
        plyReg.setRCz((int) z);
        plyReg.setRegCount(5);
        plugin.getDatabase().save(plyReg);
        return true;
    }

    public boolean CanLeftClickL(String p) {
        PrisonDataBase plyReg = OpenDataBase(p);
        if (plyReg.getRegCount() >= 1 && plyReg.getRegCount() < 2) {
            return true;
        }
        return false;
    }

    public boolean CanLeftClickS(String p) {
        PrisonDataBase plyReg = OpenDataBase(p);

        if (plyReg.getRegCount() >= 2 && plyReg.getRegCount() < 3) {
            return true;
        }
        return false;
    }

    public boolean CanLeftClickLC(String p) {
        PrisonDataBase plyReg = OpenDataBase(p);
        if (plyReg.getRegCount() >= 3 && plyReg.getRegCount() < 4) {
            return true;
        }
        return false;
    }

    public boolean CanLeftClickRC(String p) {
        PrisonDataBase plyReg = OpenDataBase(p);
        if (plyReg.getRegCount() >= 4 && plyReg.getRegCount() < 5) {
            return true;
        }
        return false;
    }

    public String GetPrisonCity(String p) {
        PrisonDataBase plyReg = OpenDataBase(p);
        return plyReg.getCityName();
    }

    public Location GetPrisonLocation(String p) {
        PrisonDataBase plyReg = OpenDataBase(p);
        return new Location(plugin.getServer().getWorld(plyReg.getWorldName()), plyReg.getPx(), plyReg.getPy(), plyReg.getPz());
    }

    public Location GetChestR(String p) {
        PrisonDataBase plyReg = OpenDataBase(p);
        return new Location(plugin.getServer().getWorld(plyReg.getWorldName()), plyReg.getLCx(), plyReg.getLCy(), plyReg.getLCz());
    }

    public Location GetChestL(String p) {
        PrisonDataBase plyReg = OpenDataBase(p);
        return new Location(plugin.getServer().getWorld(plyReg.getWorldName()), plyReg.getRCx(), plyReg.getRCy(), plyReg.getRCz());
    }

    public Location GetSign(String p) {
        PrisonDataBase plyReg = OpenDataBase(p);
        return new Location(plugin.getServer().getWorld(plyReg.getWorldName()), plyReg.getSx(), plyReg.getSy(), plyReg.getSz());
    }

    public void RemovePrison(String p) {
        PrisonDataBase plyReg = OpenDataBase(p);
        plugin.getDatabase().delete(PrisonDataBase.class, plyReg.getPrisonId());
    }
}
